﻿@using CodeCamp.Models.Account
@model WIFLogin
@{
    @* ViewBag.RegistrationUrl set in code - SetSharedViewBagProperties *@
    ViewBag.EventName = "";
    ViewBag.Title = "";
    if ((@Model != null) && (@Model.EventDisplayName != null))
    {
        ViewBag.Title = @Html.Encode(@Model.EventDisplayName + " | Signin");
        ViewBag.EventName = @Model.EventDisplayName;
    }
    else
    {
        ViewBag.Title = "Code Camp | Signin";
        ViewBag.EventName = "Code Camp";
    }
}

@section headaddon {
<style type="text/css">
body
{
	font-family: Arial, Helvetica, sans-serif;
	text-align: center;
}

div#main
{
    height:500px;
}

div.SignInWrapper
{
    padding:10px;
    width:700px;
}

div.TopBanner
{
    clear:both;
    padding:10px;
    width:700px;
    @*border: solid 1px #BBBBBB;*@
}

div.SignInInfo
{
    float: left;
    padding: 10px;
    width: 328px;
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	@*border: solid 1px #BBBBBB;*@
	position: relative;
	height: 325px;
}

div.SigninMain
{
    float: right;
    @*width: 350px;*@
}

div.SignInContent
{
    float: left;
    padding: 10px;
    width: 328px;
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	@*border: solid 1px #BBBBBB;*@
	position: relative;
	height: 325px;
}

div.Banner
{
	padding-top:10px;
	padding-bottom:10px;
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	background: #EEEEEE;
	border-top: solid 1px #BBBBBB;
	border-left: solid 1px #BBBBBB;
	border-right: solid 1px #BBBBBB;
	width: 340px;
}

div.Header
{
	padding:10px 10px;
	text-align: center;
	margin-left: auto;
	margin-right: auto;
	margin-bottom: 10px;
}

div.LeftArea
{
	padding:15px 15px;
	width: 320px; 
	height: 100%;
	position: absolute;
	top: 0px;
	left: 0px;
}

div.RightArea
{
	padding:15px 15px; 
	width: 320px; 
	height: 100%;
	position: absolute;
	top: 0px;
	left: 370px;
}

div.Divider
{
	width: 20px;
	height: 100%;
	text-align: center;
	position: absolute;
	top: 10px;
	left: 350px;
}

div.DividerLine
{
	width: 1px; 
	height: 45%;
	background: #BBBBBB;
	margin-left: auto;
	margin-right: auto;
}

button.IdentityProvider
{
	width: 250px;
	height: 50px;
	text-align: center;
	border: solid 1px #BBBBBB;
	margin-left: auto;
	margin-right: auto;
	margin-bottom: 5px;
	position: relative;
	cursor: pointer;
	font-size: 18px;
	color: blue;
	background: #F7F7F7;
	background: -webkit-gradient(linear, left top, left bottom, from(#FFFFFF), to(#EEEEEE));
	background: -moz-linear-gradient(bottom, #EEEEEE, #FFFFFF);
	filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#FFFFFF', EndColorStr='#EEEEEE');
	white-space: normal
}

img.IdentityProviderImage 
{ 
	vertical-align: middle;
	position: relative;
}

button.IdentityProvider:hover 
{ 
	background: #EEEEEE;
	background: -moz-linear-gradient(bottom, #DDDDDD, #FFFFFF);
	background: -webkit-gradient(linear, left top, left bottom, from(#FFFFFF), to(#DDDDDD));
	filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#FFFFFF', EndColorStr='#DDDDDD');
}

label
{
	color: red;
}

.footer
{
    clear:both;
    font-size: 0.95em;
    float: left;
    
    color: #888888;
    height: 70px;
    width:99%;
}

.footer A
{
    color: #888888;
    text-decoration: underline;
}
.link_divider
{
    margin-left: 5px;
    margin-right: 5px;
}
</style>
}

<div id="SignInWrapper" class="SignInWrapper">
	<div id="TopBanner" class="TopBanner"><b>Sign in to Boston Code Camp</b></div>
    <div id="SignInInfo" class="SignInInfo">
        <p><b>Click on one of the buttons to the right</b> to sign in using your account with that provider.</p>
        <p>When you sign in with your account, you may see a message about <br />bostoncodecamp.accesscontrol.windows.net.<br /> This is the service that
        manages your sign in for the Code Camp web site.</p>
        <p>Don't have an account with one of these providers and don't want to get one? @Html.ActionLink("Contact Us", "Contact", "Home", new { eventName = Model.EventShortName }, null)</p>
    <h3>Registering for Code Camp as an attendee?</h3>
    @if (string.IsNullOrWhiteSpace(ViewBag.RegistrationUrl) || (ViewBag.RegistrationUrl == "."))
    {
        <p>Please check back later.</p>
    }
    else
    {
        <h3>Click here for <a href="@ViewBag.RegistrationUrl">@ViewBag.EventName attendee registration</a>.</h3>
        <p>
            Free registration to attend @ViewBag.EventName is provided through EventBright.com.
            Space is limited,<br />so please <a href="@ViewBag.RegistrationUrl">sign up today</a>.
        </p>
    }
    </div>
   <div id="Main" style="display:none" class="SigninMain">

	    <div id="SignInContent" class="SignInContent">
		    <div id="LeftArea" class="LeftArea" style="display:none;">
			    <div class="Header">Sign in using your account on:</div>   
			    <div id="IdentityProvidersList"></div><br />
			    <div id="MoreOptions" style="display:none;"><a href="" onclick="ShowDefaultSigninPage(); return false;">Show more options</a></div>
		    </div> 
		    <div id="Divider" class="Divider" style="display:none;">
			    <div class="DividerLine"></div>
			    <div>Or</div>
			    <div class="DividerLine"></div>
		    </div>
		    <div id="RightArea" class="RightArea" style="display:none;">
			    <div class="Header">Sign in using your e-mail address:</div>
			    <form onsubmit="EmailAddressEntered(); return false;">
			        <input type="text" id="EmailAddressTextBox" />
			        <input type="submit" id="EmailAddressSubmitButton" value="Submit"/><br /><br />
			        <label id="EmailAddressError"></label>
			    </form>
		    </div>
	    </div>
        <div class="footer">
            <a id="HyperLink1" href='http://go.microsoft.com/fwlink/?LinkID=131004' target="_blank">Privacy</a>
        </div>
    </div>
</div>

@if (Model.Platform == CodeCamp.FederatedAuthConfig.PlatformType.Local)
{
    //demos.rgws008.local:38080
    <script language="javascript" type="text/javascript">
        var cookieName = "ACSChosenIdentityProvider-20002121";
    </script>
}
else if (Model.Platform == CodeCamp.FederatedAuthConfig.PlatformType.azLocal)
{
    <script language="javascript" type="text/javascript">
        var cookieName = "ACSChosenIdentityProvider-20002090";
    </script>
}
else if (Model.Platform == CodeCamp.FederatedAuthConfig.PlatformType.azhsStage)
{
    <script language="javascript" type="text/javascript">
        var cookieName = "ACSChosenIdentityProvider-20002061";
    </script>
}
else if (Model.Platform == CodeCamp.FederatedAuthConfig.PlatformType.azwsTest)
{
    <script language="javascript" type="text/javascript">
        var cookieName = "ACSChosenIdentityProvider-20002050";
    </script>
}
else
{
    //(Model.Platform == WIFLogin.PlatformType.Prod)
    //*** Production *** www.bostoncodecamp.com
    <script language="javascript" type="text/javascript">
        var cookieName = "ACSChosenIdentityProvider-20002062";
    </script>
}

    <script language="javascript" type="text/javascript">
        var identityProviders = [];
        var cookieExpiration = 30; // days
        var maxImageWidth = 240;
        var maxImageHeight = 40;

        // This function will be called back by the HRD metadata, and is responsible for displaying the sign-in page.
        function ShowSigninPage(json) {
            var cookieName = GetHRDCookieName();
            var numIdentityProviderButtons = 0;
            var showEmailEntry = false;
            var showMoreOptionsLink = false;
            identityProviders = json;

            if (identityProviders.length == 0) {
                var mainDiv = document.getElementById("SignInContent");
                mainDiv.appendChild(document.createElement("br"));
                mainDiv.appendChild(document.createTextNode("Error: No identity providers are associated with this application."));
            }

            // Loop through the identity providers
            for (var i in identityProviders) {
                // Show all sign-in options if no cookie is set
                if (cookieName === null) {
                    if (identityProviders[i].EmailAddressSuffixes.length > 0) {
                        showEmailEntry = true;
                    }
                    else {
                        // Only show a button if there is no email address configured for this identity provider.
                        CreateIdentityProviderButton(identityProviders[i]);
                        numIdentityProviderButtons++;
                    }
                }
                // Show only the last selected identity provider if a cookie is set
                else {
                    if (cookieName == identityProviders[i].Name) {
                        CreateIdentityProviderButton(identityProviders[i]);
                        numIdentityProviderButtons++;
                    }
                    else {
                        showMoreOptionsLink = true;
                    }
                }
            }
            //If the user had a cookie but it didn't match any current identity providers, show all sign-in options 
            if (cookieName !== null && numIdentityProviderButtons === 0 && !showEmailEntry) {
                ShowDefaultSigninPage();
            }
            //Othewise, render the sign-in page normally
            else {
                ShowSigninControls(numIdentityProviderButtons, showEmailEntry, showMoreOptionsLink);
            }

            document.getElementById("Main").style.display = "";
        }

        // Resets the sign-in page to its original state before the user logged in and received a cookie.
        function ShowDefaultSigninPage() {
            var numIdentityProviderButtons = 0;
            var showEmailEntry = false;
            document.getElementById("IdentityProvidersList").innerHTML = "";
            for (var i in identityProviders) {
                if (identityProviders[i].EmailAddressSuffixes.length > 0) {
                    showEmailEntry = true;
                }
                else {
                    CreateIdentityProviderButton(identityProviders[i]);
                    numIdentityProviderButtons++;
                }
            }
            ShowSigninControls(numIdentityProviderButtons, showEmailEntry, false);
        }

        //Reveals the sign-in controls on the sign-in page, and ensures they are sized correctly
        function ShowSigninControls(numIdentityProviderButtons, showEmailEntry, showMoreOptionsLink) {

            //Display the identity provider links, and size the page accordingly
            if (numIdentityProviderButtons > 0) {
                document.getElementById("LeftArea").style.display = "";
                if (numIdentityProviderButtons > 4) {
                    var height = 325 + ((numIdentityProviderButtons - 4) * 55);
                    document.getElementById("SignInContent").style.height = height + "px";
                }
            }
            //Show an email entry form if email mappings are configured
            if (showEmailEntry) {
                document.getElementById("RightArea").style.display = "";
                if (numIdentityProviderButtons === 0) {
                    document.getElementById("RightArea").style.left = "0px";
                    document.getElementById("LeftArea").style.display = "none";
                }
            }
            //Show a link to redisplay all sign-in options
            if (showMoreOptionsLink) {
                document.getElementById("MoreOptions").style.display = "";
            }
            else {
                document.getElementById("MoreOptions").style.display = "none";
            }
            //Resize the page if multiple sign-in options are present
            if (numIdentityProviderButtons > 0 && showEmailEntry) {
                document.getElementById("SignInContent").style.width = "720px";
                document.getElementById("Banner").style.width = "720px";
                document.getElementById("Divider").style.display = "";
            }

        }

        //Creates a stylized link to an identity provider's login page
        function CreateIdentityProviderButton(identityProvider) {
            var idpList = document.getElementById("IdentityProvidersList");
            var button = document.createElement("button");
            button.setAttribute("name", identityProvider.Name);
            button.setAttribute("id", identityProvider.LoginUrl);
            button.className = "IdentityProvider";
            button.onclick = IdentityProviderButtonClicked;

            // Display an image if an image URL is present
            if (identityProvider.ImageUrl.length > 0) {

                var img = document.createElement("img");
                img.className = "IdentityProviderImage";
                img.setAttribute("src", identityProvider.ImageUrl);
                img.setAttribute("alt", identityProvider.Name);
                img.setAttribute("border", "0");
                img.onLoad = ResizeImage(img);

                button.appendChild(img);
            }
            // Otherwise, display a text link if no image URL is present
            else if (identityProvider.ImageUrl.length === 0) {
                button.appendChild(document.createTextNode(identityProvider.Name));
            }

            idpList.appendChild(button);
        }

        // Gets the name of the remembered identity provider in the cookie, or null if there isn't one.
        function GetHRDCookieName() {
            var cookie = document.cookie;
            if (cookie.length > 0) {
                var cookieStart = cookie.indexOf(cookieName + "=");
                if (cookieStart >= 0) {
                    cookieStart += cookieName.length + 1;
                    var cookieEnd = cookie.indexOf(";", cookieStart);
                    if (cookieEnd == -1) {
                        cookieEnd = cookie.length;
                    }
                    return unescape(cookie.substring(cookieStart, cookieEnd));
                }
            }
            return null;
        }

        // Sets a cookie with a given name
        function SetCookie(name) {
            var expiration = new Date();
            expiration.setDate(expiration.getDate() + cookieExpiration);
            var secure = "";

            // If your application uses SSL, consider setting secure=";secure".

            document.cookie = cookieName + "=" + escape(name) + ";expires=" + expiration.toUTCString() + secure;
        }

        // Sets a cookie to remember the chosen identity provider and navigates to it.
        function IdentityProviderButtonClicked() {
            SetCookie(this.getAttribute("name"));
            window.location = this.getAttribute("id");
            return false;
        }

        function SetEmailError(string) {
            var EmailAddressError = document.getElementById("EmailAddressError");
            if (EmailAddressError.hasChildNodes()) {
                EmailAddressError.replaceChild(document.createTextNode(string), EmailAddressError.firstChild);
            }
            else {
                EmailAddressError.appendChild(document.createTextNode(string));
            }
        }

        function EmailAddressEntered() {
            var enteredEmail = document.getElementById("EmailAddressTextBox").value;
            var identityProvider = null;
            if (enteredEmail.length === 0) {
                SetEmailError("Please enter an e-mail address.");
                return;
            }

            if (enteredEmail.indexOf("@@") <= 0) {
                SetEmailError("Please enter a valid e-mail address.");
                return;
            }

            var enteredDomain = enteredEmail.split("@@")[1].toLowerCase();
            for (var i in identityProviders) {
                for (var j in identityProviders[i].EmailAddressSuffixes) {
                    if (enteredDomain == identityProviders[i].EmailAddressSuffixes[j].toLowerCase()) {
                        identityProvider = identityProviders[i];
                    }
                }
            }

            if (identityProvider === null) {
                SetEmailError("" + "'" + enteredDomain + "'" + " is not a recognized e-mail domain.");
                return;
            }

            // If we have gotten this far the e-mail address suffix was recognized. Write a cookie and redirect to the login URL.
            SetCookie(identityProvider.Name);
            window.location = identityProvider.LoginUrl;
        }

        // If the image is larger than the button, scale maintaining aspect ratio.
        function ResizeImage(img) {
            if (img.height > maxImageHeight || img.width > maxImageWidth) {
                var resizeRatio = 1;
                if (img.width / img.height > maxImageWidth / maxImageHeight) {
                    // Aspect ratio wider than the button
                    resizeRatio = maxImageWidth / img.width;
                }
                else {
                    // Aspect ratio taller than or equal to the button
                    resizeRatio = maxImageHeight / img.height;
                }

                img.setAttribute("height", img.height * resizeRatio);
                img.setAttribute("width", img.width * resizeRatio);
            }
        }

    </script>
    <!-- This script gets the HRD metadata in JSON and calls the callback function which renders the links -->
    <script src="https://bostoncodecamp.accesscontrol.windows.net:443/v2/metadata/IdentityProviders.js?protocol=wsfederation&amp;realm=@(Model.IdentityRealm)&amp;reply_to=&amp;context=&amp;request_id=&amp;version=1.0&amp;callback=ShowSigninPage" type="text/javascript"></script>
    
